package com.ruoyi.project.dj.controller;

import java.util.List;

import com.ruoyi.project.dj.domain.DjPoliticalBirthday;
import com.ruoyi.project.dj.service.IDjPoliticalBirthdayService;
import com.ruoyi.project.system.partymember.service.IPartyMemberService;
import com.ruoyi.project.system.partymember.service.impl.PartyMemberServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.text.Convert;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;

/**
 * 政治生日Controller
 * 
 * @author ruoyi
 * @date 2023-07-15
 */
@Controller
@RequestMapping("/dj/political/birthday")
public class DjPoliticalBirthdayController extends BaseController
{
    private String prefix = "dj/political/birthday";

    @Autowired
    private IDjPoliticalBirthdayService djPoliticalBirthdayService;

    @Autowired
    private IPartyMemberService partyMemberService;

    @RequiresPermissions("dj:political:birthday:view")
    @GetMapping()
    public String birthday()
    {
        return prefix + "/birthday";
    }

    /**
     * 查询政治生日列表
     */
    @RequiresPermissions("dj:political:birthday:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DjPoliticalBirthday djPoliticalBirthday)
    {
        startPage();
        List<DjPoliticalBirthday> list = djPoliticalBirthdayService.selectDjPoliticalBirthdayList(djPoliticalBirthday);
        return getDataTable(list);
    }

    /**
     * 导出政治生日列表
     */
    @RequiresPermissions("dj:political:birthday:export")
    @Log(title = "政治生日", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DjPoliticalBirthday djPoliticalBirthday)
    {
        List<DjPoliticalBirthday> list = djPoliticalBirthdayService.selectDjPoliticalBirthdayList(djPoliticalBirthday);
        ExcelUtil<DjPoliticalBirthday> util = new ExcelUtil<DjPoliticalBirthday>(DjPoliticalBirthday.class);
        return util.exportExcel(list, "政治生日数据");
    }

    /**
     * 新增政治生日
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存政治生日
     */
    @RequiresPermissions("dj:political:birthday:add")
    @Log(title = "政治生日", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DjPoliticalBirthday djPoliticalBirthday)
    {
        try {
            // 处理多选党员的情况
            if (djPoliticalBirthday.getParticipants() != null && !djPoliticalBirthday.getParticipants().isEmpty()) {
                // 如果有多选党员，使用多选数据
                String[] memberIds = djPoliticalBirthday.getParticipants().split(",");
                if (memberIds.length > 0) {
                    // 设置第一个党员为主要党员（兼容原有字段）
                    djPoliticalBirthday.setPartyMemberId(Long.valueOf(memberIds[0].trim()));
                    if (djPoliticalBirthday.getParticipantNames() != null && !djPoliticalBirthday.getParticipantNames().isEmpty()) {
                        String[] memberNames = djPoliticalBirthday.getParticipantNames().split(",");
                        if (memberNames.length > 0) {
                            djPoliticalBirthday.setMemberName(memberNames[0].trim());
                        }
                    }
                }
            } else {
                // 如果没有党员名称，则根据id查找党员姓名
                if (djPoliticalBirthday.getMemberName() == null || "".equals(djPoliticalBirthday.getMemberName())) {
                    if (djPoliticalBirthday.getPartyMemberId() != null) {
                        djPoliticalBirthday.setMemberName(partyMemberService.selectPartyMemberById(djPoliticalBirthday.getPartyMemberId()).getName());
                    }
                }
            }

            return toAjax(djPoliticalBirthdayService.insertDjPoliticalBirthday(djPoliticalBirthday));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改政治生日
     */
    @RequiresPermissions("dj:political:birthday:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DjPoliticalBirthday djPoliticalBirthday = djPoliticalBirthdayService.selectDjPoliticalBirthdayById(id);
        mmap.put("djPoliticalBirthday", djPoliticalBirthday);
        return prefix + "/edit";
    }

    /**
     * 修改保存政治生日
     */
    @RequiresPermissions("dj:political:birthday:edit")
    @Log(title = "政治生日", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DjPoliticalBirthday djPoliticalBirthday)
    {
        try {
            // 处理多选党员的情况
            if (djPoliticalBirthday.getParticipants() != null && !djPoliticalBirthday.getParticipants().isEmpty()) {
                // 如果有多选党员，使用多选数据
                String[] memberIds = djPoliticalBirthday.getParticipants().split(",");
                if (memberIds.length > 0) {
                    // 设置第一个党员为主要党员（兼容原有字段）
                    djPoliticalBirthday.setPartyMemberId(Long.valueOf(memberIds[0].trim()));
                    if (djPoliticalBirthday.getParticipantNames() != null && !djPoliticalBirthday.getParticipantNames().isEmpty()) {
                        String[] memberNames = djPoliticalBirthday.getParticipantNames().split(",");
                        if (memberNames.length > 0) {
                            djPoliticalBirthday.setMemberName(memberNames[0].trim());
                        }
                    }
                }
            } else {
                // 单选党员的情况，清空多选相关字段
                djPoliticalBirthday.setParticipants(null);
                djPoliticalBirthday.setParticipantNames(null);
                
                // 如果没有党员名称，则根据id查找党员姓名
                if (djPoliticalBirthday.getMemberName() == null || djPoliticalBirthday.getMemberName().isEmpty()) {
                    if (djPoliticalBirthday.getPartyMemberId() != null) {
                        djPoliticalBirthday.setMemberName(partyMemberService.selectPartyMemberById(djPoliticalBirthday.getPartyMemberId()).getName());
                    }
                }
            }
            return toAjax(djPoliticalBirthdayService.updateDjPoliticalBirthday(djPoliticalBirthday));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 删除政治生日
     */
    @RequiresPermissions("dj:political:birthday:remove")
    @Log(title = "政治生日", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(djPoliticalBirthdayService.deleteDjPoliticalBirthdayByIds(ids));
    }
    
    /**
     * 查看政治生日明细
     */
    @GetMapping("/view/{id}")
    @Log(title = "政治生日", businessType = BusinessType.OTHER)
    public String view(@PathVariable("id") Long id, ModelMap mmap)
    {
        DjPoliticalBirthday djPoliticalBirthday = djPoliticalBirthdayService.selectDjPoliticalBirthdayById(id);
        mmap.put("djPoliticalBirthday", djPoliticalBirthday);
        return prefix + "/view";
    }
}